<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    id='enterName'
    :before-close="handleClose">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="品牌名称" prop="roleName">
        <el-input v-model="dataForm.brandName" placeholder="请输入品牌名称"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        brandName: ''
      },
      dataRule: {
        brandName: [
          { required: true, message: '品牌名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose(){
      this.dataForm.brandName='';
      this.dataForm.id='';
      this.visible = false
    },
    // 查询初始化数据
    init (id) {
      this.visible = true
      this.dataForm.id = id || 0
      console.log(this.dataForm.id)
      if (this.dataForm.id !== 0) {
        this.$http({
          url: this.$http.adornUrl(`/boss/brand/info/${this.dataForm.id}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          console.log(data)
          if (data && data.code === 1) {
            this.dataForm.brandName = data.data.brandName
          }
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/boss/brand/save'),
            method: 'post',
            data: this.$http.adornData({
              'brandId': this.dataForm.id || undefined,
              'brandName': this.dataForm.brandName
            })
          }).then(({data}) => {
            if (data && data.code === 1) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.handleClose();
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
